import WxValidate from '../../../../lib/WxValidate.js'

const App = getApp()
import Request from '../../../../utils/request.js'
let app = getApp()
let store = app.store


// pages/me/manage/addCard.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contactName: '',
    contactMobile: '',
    address: '', //详细地址
    getCodeText: '获取验证码',
    isDefault: false,
    addressZone: ['', '', ''],
    isAdd: false,
    addressId: '',
    postCode: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    if (Object.keys(options).length === 0) { //新增模式
      this.setData({
        isAdd: true
      })
    } else { //编辑模式
      let data = { ...this.data,
        ...JSON.parse(options.data)
      };
      console.log(data);
      data.addressZone = [data.addressProvince, data.addressCity, data.addressDistrict];
      this.setData(data)
      this.setData({
        isAdd: false
      })
    }


    //验证方法
    this.initValidate();
  },

  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      addressZone: e.detail.value
    })
  },


  /***验证表单字段 */
  initValidate: function() {
    const rules = {
      contactName: {
        required: true,
        maxlength: 10,
      },
      contactMobile: {
        required: true,
        tel: true
      },
      addressZone: {
        required: true
      },
      address: {
        required: true,
        noEmoji: true
      },
      postCode: {
        maxlength: 6,
        digits: true
      }
    }
    const messages = {
      contactName: {
        required: '请填写名称',
      },
      contactMobile: {
        required: '请填写联系电话',
        tel: '请填写正确的联系电话'
      },
      addressZone: {
        required: '请填写地址',
      },
      address: {
        required: '请填写详细地址',
        noEmoji: '详细地址不能输入表情',
      },
      postCode: {
        maxlength: '邮编最多可以输入6个字符',
        digits: '邮编只能输入数字',
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },

  /***调用验证函数***/
  formSubmit: function(e) {
    console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    const params = e.detail.value

    //校验表单"^[ ]+$"
    if (params.contactName.indexOf(" ") > -1 || params.contactName == "") {
      // this.showModal("请填写正确的姓名")
      wx.showModal({
        content: "请填写正确的姓名"
      })
      // console.log("?????????", params.contactName)
      return false
    }else{
      // console.log("!!!!!!!!!!")
      // return false
    }

    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      // debugger
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }

    wx.showLoading({
      title: '保存中...',
    });

    // 数据处理

    params.addressCity = params.addressZone[1];
    params.addressProvince = params.addressZone[0];
    params.addressDistrict = params.addressZone[2];
    params.addressZone = String(params.addressProvince + params.addressCity + params.addressDistrict);
    params.patientId = store.getState().systemLoginData.imId
    if (this.data.isAdd) {
      this.creatRequest(params);
    } else {
      this.editRequest(params);
    }


  },

  editRequest(params) {
    params.addressId = this.data.addressId;
    console.log(params);

    let self = this;
    //向后台发送时数据 wx.request...
    let data = Request.postRequest('/patientAddress/update', params).then(data => {
      self.setData({
        items: data.data
      })
      wx.hideLoading()

      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 1000
      })

      setTimeout(() => {
        // wx.navigateBack({
        //   url: '/pages/me/address/address'
        // })
        wx.navigateBack()
      }, 500)

    })
  },

  creatRequest(params) {
    let self = this;
    //向后台发送时数据 wx.request...
    let p = JSON.stringify(params);
    let data = Request.postRequest('/patientAddress/insert', p).then(data => {
      self.setData({
        items: data.data
      })
      wx.hideLoading()

      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 1000
      })

      setTimeout(() => {
        wx.navigateBack({
          url: '/pages/me/address/address'
        })
      }, 500)

    })
  },

  /***报错 **/
  showModal(error) {
    wx.showModal({
      content: error.msg
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})